package org.example.auth.listener;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.http.useragent.UserAgent;
import cn.hutool.http.useragent.UserAgentUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.example.api.system.RemoteLoginLogService;
import org.example.api.system.domain.bo.RemoteLoginLogBo;
import org.example.auth.event.LoginLogEvent;
import org.example.common.core.constants.Constants;
import org.example.common.core.utils.StringUtils;
import org.example.common.core.utils.ip.AddressUtils;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * 异步调用日志服务
 *
 */
@Component
@Slf4j
public class LoginLogEventListener {

    @DubboReference
    private RemoteLoginLogService remoteLoginLogService;

    /**
     * 保存系统登录日志记录
     */
    @Async
    @EventListener
    public void saveLog(LoginLogEvent logininforEvent) {
        HttpServletRequest request = logininforEvent.getRequest();
        final String ip = AddressUtils.getIpAddr(request);
        // 客户端信息
        String address = AddressUtils.getRealAddressByIP(ip);
        StringBuilder s = new StringBuilder();
        s.append(getBlock(ip));
        s.append(address);
        s.append(getBlock(logininforEvent.getUsername()));
        s.append(getBlock(logininforEvent.getStatus()));
        s.append(getBlock(logininforEvent.getMessage()));
        // 打印信息到日志
        log.info(s.toString(), logininforEvent.getArgs());
        // 封装对象
        RemoteLoginLogBo logininfor = new RemoteLoginLogBo();
        logininfor.setTenantId(logininforEvent.getTenantId());
        logininfor.setUserName(logininforEvent.getUsername());
        logininfor.setDeviceType(logininforEvent.getDeviceType());
        logininfor.setIpaddr(ip);
        logininfor.setLoginLocation(address);
        logininfor.setMsg(logininforEvent.getMessage());
        if(ObjectUtil.isNotNull(request)&&ObjectUtil.isNotEmpty(request.getHeader("User-Agent"))){
            // 获取客户端操作系统
            final UserAgent userAgent = UserAgentUtil.parse(request.getHeader("User-Agent"));
            String os = userAgent.getOs().getName();
            // 获取客户端浏览器
            String browser = userAgent.getBrowser().getName();
            logininfor.setBrowser(browser);
            logininfor.setOs(os);
        }
        // 日志状态
        if (StringUtils.equalsAny(logininforEvent.getStatus(), Constants.LOGIN_SUCCESS, Constants.LOGOUT, Constants.REGISTER)) {
            logininfor.setStatus(Constants.SUCCESS);
        } else if (Constants.LOGIN_FAIL.equals(logininforEvent.getStatus())) {
            logininfor.setStatus(Constants.FAIL);
        }
        remoteLoginLogService.saveLoginLog(logininfor);

    }

    private String getBlock(Object msg) {
        if (msg == null) {
            msg = "";
        }
        return "[" + msg + "]";
    }

}
